package gameFight;

/**
 * 作者:TC
 * 时间:定义基础角色类 18:07
 * 描述:没有bug
 */
// 定义一个基础角色类
public class Role {
    protected int health;  // 生命值
    protected int attack;  // 攻击力
    protected int defense; // 防御力
    protected double critRate;  // 暴击率
    protected double shieldRate;  // 护盾触发率

    public Role(int health, int attack, int defense, double critRate, double shieldRate) {
        this.health = health;
        this.attack = attack;
        this.defense = defense;
        this.critRate = critRate;
        this.shieldRate = shieldRate;
    }

    public int getHealth() {
        return health;
    }

    public void setHealth(int health) {
        this.health = health;
    }

    public int getAttack() {
        return attack;
    }
    public void setAttack(int attack) {
        this.attack = attack;
    }
    public int getDefense() {
        return defense;
    }
    public void setDefense(int defense) {
        this.defense = defense;
    }
    public double getCritRate() {
        return critRate;
    }
    public void setCritRate(double critRate) {
        this.critRate = critRate;
    }

    public double getShieldRate() {
        return shieldRate;
    }

    public void setShieldRate(double shieldRate) {
        this.shieldRate = shieldRate;
    }

    // 计算角色的实际攻击力,在每一个角色类中进行重写每个角色不同的攻击和防御方法
    public int calculateAttack() {
        if (Math.random() <= critRate) {
            return (int) (attack * 1.5);
        } else {
            return attack;
        }
    }

    // 角色受击时计算实际防御力
    public int calculateDefense() {
        if (Math.random() <= shieldRate) {
            return defense + 10;
        } else {
            return defense;
        }
    }

    // 角色受到伤害后生命值减少
    public void takeDamage(int damage) {
        health -= damage;
        if (health < 0) {
            health = 0;
        }
    }

    // 判断角色是否存活
    public boolean isAlive() {
        return health > 0;
    }
}
